package com.mingqijia.gassafety.statistics.controller;

import com.alibaba.fastjson.JSONObject;
import com.gap.basic.base.CommonResponse;
import com.mingqijia.gassafety.db.mapper.DtuMapper;
import com.mingqijia.gassafety.shared.constant.EquipmentType;
import com.mingqijia.gassafety.shared.utils.HeaderUtil;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.statistics.request.*;
import com.mingqijia.gassafety.statistics.response.*;
import com.mingqijia.gassafety.statistics.service.IndustryScreenService;
import com.mingqijia.gassafety.webserver.request.CustDetailRequest;
import com.mingqijia.gassafety.webserver.request.RecordLogRequest;
import com.mingqijia.gassafety.webserver.response.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @program: gassafety
 * @description:
 * @author: Mr.Wang
 * @create: 2022-12-12 10:06
 **/


@Slf4j
@Api(tags = "工商业大屏")
@RestController(value = "IndustryScreenController")
@RequestMapping("/business/v1/industryScreen")
public class IndustryScreenController {

    @Autowired
    HeaderUtil headerUtil;
    @Autowired
    DtuMapper dMapper;
    @Autowired
    IndustryScreenService industryScreenService;

    /**
     * 接入用户数数据
     * @return
     */
    @PostMapping("/underUserSum")
    @ApiOperation(value = "接入用户数")
    public CommonResponse<Object> underUserSum(@RequestBody(required = false) IndustryScreenReq screenReq) {
        return CommonResponse.success(industryScreenService.underUserSum(screenReq));
    }

    /**
     * 设备运行情况
     * @return
     */
    @PostMapping("/deviceRunningCase")
    @ApiOperation(value = "设备运行情况")
    public CommonResponse<IndustryDeviceRunningCaseResp> deviceRunningCase(@RequestBody(required = false) IndustryScreenReq screenReq) {
        return CommonResponse.success(industryScreenService.deviceRunningCase(screenReq));
    }

    /**
     * 工单情况
     * type=0 今日  type =1 一周
     *
     * @return
     */
    @PostMapping("/orderCondition")
    @ApiOperation(value = "工单情况")
    public CommonResponse<Object> orderCondition(@RequestBody IndustryScreenOrderConditionReq req) {
        return CommonResponse.success(industryScreenService.orderCondition(req));
    }

    @PostMapping("/map/group")
    @ApiOperation(value = "大屏分组组合数据")
    public CommonResponse<ConsumerMapGroupResponse> queryCustMapGroup(@RequestBody @Valid IndustryMapGroupRequest request) {
        return CommonResponse.success(industryScreenService.queryCustMapGroupList(request));
    }

    @PostMapping("/map/list")
    @ApiOperation(value = "地图电子围栏数据")
    public CommonResponse<ConsumerMapResponse> queryCustMapList(@RequestBody(required = false) @Valid IndustryConsumerMapRequest request) {
        return CommonResponse.success(industryScreenService.queryBoundaryCustList(request));
    }

    @ApiOperation(value = "告警信息")
    @PostMapping("/warningInfo")
    public CommonResponse<List<IndustryConsumerResponse>> warningInfo(@RequestBody IndustryScreenWarningReq warningReq) {

        List<IndustryConsumerResponse> response = industryScreenService.warningInfoGas(warningReq);
        return CommonResponse.success(response);
    }

    /**
     * 接入用户列表
     * @return
     */
    @PostMapping("underUserList")
    @ApiOperation(value = "接入用户列表")
    public CommonResponse<Object> underUserList(@RequestBody(required = false) IndustryUnderUserListReq userListReq) {
        return CommonResponse.success(industryScreenService.underUserList(userListReq));
    }

    @ApiOperation(value = "大屏客户设备详情")
    @PostMapping("/consumerDetail")
    public CommonResponse<ConsumerDetailResponse> consumerDetail(@RequestBody @Valid CustDetailRequest request) {
        log.info("consumerDetail--request:{}", JSONObject.toJSONString(request));
        ConsumerDetailResponse response = industryScreenService.consumerDetailGas(request);
        return CommonResponse.success(response);
    }

    /**
     * 未处理工单数
     *
     * @return
     */
    @PostMapping("getConditionSum")
    @ApiOperation(value = "未处理工单数")
    public CommonResponse<Object> getConditionSum(@RequestBody IndustryScreenWarningReq warningReq) {
        return CommonResponse.success(industryScreenService.getConditionSum(warningReq));
    }

    /**
     * 告警信息数量
     * @param warningReq
     * @return
     */
    @ApiOperation(value = "告警信息数量")
    @PostMapping("/warningSum")
    public CommonResponse<Object> warningSum(@RequestBody IndustryScreenWarningReq warningReq) {

        Map<String, Integer> warningSum = industryScreenService.warningSum(warningReq);
        return CommonResponse.success(warningSum);
    }

    @ApiOperation(value = "大屏客户设备详情")
    @PostMapping("/consumerDeviceDetail")
    public CommonResponse<ConsumerDeviceDetailResp> consumerDeviceDetail(@RequestBody @Valid CustDetailRequest request) {
        log.info("consumerDetail--request:{}", JSONObject.toJSONString(request));
        ConsumerDeviceDetailResp response = industryScreenService.consumerDeviceDetail(request);
        return CommonResponse.success(response);
    }

    @ApiOperation("报警记录")
    @RequestMapping(value = "alert", method = RequestMethod.POST)
    public CommonResponse<PageUtil<IndustryAlertLogResp>> alert(@RequestBody IndustryRecordRequest req){
        return CommonResponse.success(industryScreenService.getAlarmList(req));
    }


    @ApiOperation("故障记录")
    @RequestMapping(value = "broken", method = RequestMethod.POST)
    public CommonResponse<PageUtil<IndustryBrokenLogResp>> broken(@RequestBody IndustryRecordRequest req){
        return CommonResponse.success(industryScreenService.broken(req));
    }
}
